package com.atguigu.tingshu.album.listener;

import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
import com.atguigu.tingshu.album.mapper.AlbumStatMapper;
import com.atguigu.tingshu.album.mapper.TrackStatMapper;
import com.atguigu.tingshu.common.config.config.RabbitConstant;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.AlbumStat;
import com.atguigu.tingshu.model.album.TrackStat;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rabbitmq.client.Channel;
import com.tencentcloudapi.ame.v20190916.models.Album;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.IllegalFormatCodePointException;
import java.util.concurrent.TimeUnit;

@Component
public class AlbumListener{

    @Autowired
    TrackStatMapper trackStatMapper;
    @Autowired
    AlbumStatMapper albumStatMapper;

    @Autowired
    RedisTemplate redisTemplate;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(RabbitConstant.USER_STAT_QUEUE),
            exchange = @Exchange(value = RabbitConstant.USER_EXCHANGE,type = ExchangeTypes.TOPIC,ignoreDeclarationExceptions ="true" ),
            key = RabbitConstant.USER_STAT_RK
    ))
    public void trackStat(TrackStatMqVo statMqVo, Channel channel, Message message)  {
        try {
            if (statMqVo==null){
                return;
            }
            //判断消息是否重复消费

            Boolean aBoolean = this.redisTemplate.opsForValue().setIfAbsent(RedisConstant.TRACK_STAT_PREFIX + statMqVo.getBusinessNo()
                    , ""
                    , RedisConstant.CACHE_TIMEOUT
                    , TimeUnit.SECONDS);
            if (!aBoolean){
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
                return;
            }


            //给声音对应的统计信息进行累加
            TrackStat trackStat = this.trackStatMapper.selectOne(new LambdaQueryWrapper<TrackStat>()
                    .eq(TrackStat::getTrackId, statMqVo.getTarckId())
                    .eq(TrackStat::getStatType, statMqVo.getStatType()));
            trackStat.setStatNum(trackStat.getStatNum()+statMqVo.getCount());
            this.trackStatMapper.updateById(trackStat);
            //给专辑进行累加累加播放量和评论数
            if (StringUtils.equals(SystemConstant.TRACK_STAT_PLAY,statMqVo.getStatType())){
                AlbumStat albumStat = this.albumStatMapper.selectOne(new LambdaQueryWrapper<AlbumStat>()
                        .eq(AlbumStat::getAlbumId, statMqVo.getAlbumId())
                        .eq(AlbumStat::getStatType, statMqVo.getStatType()));
                albumStat.setStatNum(albumStat.getStatNum()+statMqVo.getCount());
                this.albumStatMapper.updateById(albumStat);
            }

            if (StringUtils.equals(SystemConstant.TRACK_STAT_COMMENT,statMqVo.getStatType())){
                AlbumStat albumStat = this.albumStatMapper.selectOne(new LambdaQueryWrapper<AlbumStat>()
                        .eq(AlbumStat::getAlbumId, statMqVo.getAlbumId())
                        .eq(AlbumStat::getStatType, statMqVo.getStatType()));
                albumStat.setStatNum(albumStat.getStatNum()+statMqVo.getCount());
                this.albumStatMapper.updateById(albumStat);
            }


        }catch (Exception e){
           //做重试
            this.redisTemplate.delete(RedisConstant.TRACK_STAT_PREFIX+statMqVo.getBusinessNo());



        }



    }

}
